package gwin.com.numbers;

import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * Created by YuZui on 2020/6/18 0018.
 */
public class Game {
    public static final int EASY = 9;
    public static final int NORMAL = 5;
    public static final int HARD = 3;
    public boolean isGameOver;
    final int LENGTH = 9;
    int curX = 0;
    int curY = 0;
    int fact = EASY;//值越小挖走越多数
    int unknownCount = 0;

    Grid[][] grids = new Grid[LENGTH][LENGTH];


    public interface GameListener {
        void onGameOver(boolean isSuccess);

        void onGameStart();
    }

    GameListener gameListener;

    public void setGameListener(GameListener gameListener) {
        this.gameListener = gameListener;
    }

    void putNumbers() {
        long timeStart = System.currentTimeMillis();
        // 初始化中间的九宫格
        centerInit();
        crossInit();
        cornerInit();
        Log.e("zui", "耗时：" + (System.currentTimeMillis() - timeStart));
    }

    private void reset() {
        isGameOver = false;
        curX = 0;
        curY = 0;
        unknownCount = 0;
        for (int i = 0; i < LENGTH; i++) {
            for (int j = 0; j < LENGTH; j++) {
                if (grids[i][j] == null) {
                    grids[i][j] = new Grid(this, i, j);
                } else {
                    grids[i][j].reset(this);
                }
            }
        }
    }

    public void init() {
        isGameOver = false;
        reset();
        putNumbers();
        letSomeNumUnknown();
    }

    private void letSomeNumUnknown() {
        for (int i = 0; i < LENGTH; i++) {
            for (int j = 0; j < LENGTH; j++) {
                grids[i][j].fillNumber = grids[i][j].number;
                if (new Random().nextInt(fact) == 0) {
                    grids[i][j].setAsUnknown();
                    unknownCount++;
                }
            }
        }
    }

    public void gameOver(boolean isSuccess) {
        isGameOver = true;
        if (gameListener != null) {
            gameListener.onGameOver(isSuccess);
        }

    }

    private void showAnswer() {
        for (int i = 0; i < LENGTH; i++) {
            for (int j = 0; j < LENGTH; j++) {
                grids[i][j].fillNumber = grids[i][j].number;
            }
        }
    }


    public void fillNumber(int number) {
        grids[curX][curY].eraseNumber();
        grids[curX][curY].fillNumber(number);
    }

    public void eraseNumber() {
        grids[curX][curY].eraseNumber();
    }

    public void right() {
        if (curX < LENGTH - 1) {
            curX++;
        } else {
            curX = 0;
        }
    }

    public void left() {
        if (curX > 0) {
            curX--;
        } else {
            curX = LENGTH - 1;
        }
    }

    public void down() {
        if (curY < LENGTH - 1) {
            curY++;
        } else {
            curY = 0;
        }
    }

    public void up() {
        if (curY > 0) {
            curY--;
        } else {
            curY = LENGTH - 1;
        }
    }

    public void testGameOver() {
        for (int i = 0; i < LENGTH; i++) {
            for (int j = 0; j < LENGTH; j++) {
                if (grids[i][j].fillNumber == 0 || grids[i][j].sameList.size() > 0) {
                    //只要有一个没填入数字，或者错误没清空都不会触发结束
                    return;
                }
            }
        }
        gameOver(true);
    }


    /*******以下是初始化算法部分**********/
    // 初始化中间的九宫格
    void centerInit() {
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= LENGTH; ++i){
            list.add(i);
        }
        Collections.shuffle(list);
        for (int i = 3; i < 6; ++i) {
            for (int j = 3; j < 6; ++j) {
                grids[i][j].number = list.get(0);
                list.remove(0);
            }
        }
    }

    // 由中间的九宫格交叉变换，初始化上下左右四个九宫格
    void crossInit()
    {
        for (int i = 3; i < 6; ++i)
        {
            int l = 0;
            for (int j = 3; j < 6; ++j)
            {
                if (i == 3)
                {
                    grids[i + 1][l].number = grids[i][j].number;
                    grids[i + 2][l + 6].number = grids[i][j].number;
                    ++l;
                }
                else if (i == 4)
                {
                    grids[i + 1][l].number = grids[i][j].number;
                    grids[i - 1][l + 6].number = grids[i][j].number;
                    ++l;
                }
                else if (i == 5)
                {
                    grids[i - 2][l].number = grids[i][j].number;
                    grids[i - 1][l + 6].number = grids[i][j].number;
                    ++l;
                }
            }
        }
        for (int j = 3; j < 6; ++j)
        {
            int l = 0;
            for (int i = 3; i < 6; ++i)
            {
                if (j == 3)
                {
                    grids[l][j + 1].number = grids[i][j].number;
                    grids[l + 6][j + 2].number = grids[i][j].number;
                    ++l;
                }
                else if (j == 4)
                {
                    grids[l][j + 1].number = grids[i][j].number;
                    grids[l + 6][j - 1].number = grids[i][j].number;
                    ++l;
                }
                else if (j == 5)
                {
                    grids[l][j - 2].number = grids[i][j].number;
                    grids[l + 6][j - 1].number = grids[i][j].number;
                    ++l;
                }
            }
        }
    }

    // 初始化四个角上的四个九宫格
    void cornerInit()
    {
        for (int i = 0; i < 3; ++i)
        {
            int l = 0;
            for (int j = 3; j < 6; ++j)
            {
                if (i == 0)
                {
                    grids[i + 1][l].number = grids[i][j].number;
                    grids[i + 2][l + 6].number = grids[i][j].number;
                    ++l;
                }
                else if (i == 1)
                {
                    grids[i + 1][l].number = grids[i][j].number;
                    grids[i - 1][l + 6].number = grids[i][j].number;
                    ++l;
                }
                else if (i == 2)
                {
                    grids[i - 2][l].number = grids[i][j].number;
                    grids[i - 1][l + 6].number = grids[i][j].number;
                    ++l;
                }
            }
        }
        for (int i = 6; i < 9; ++i)
        {
            int l = 0;
            for (int j = 3; j < 6; ++j)
            {
                if (i == 6)
                {
                    grids[i + 1][l].number = grids[i][j].number;
                    grids[i + 2][l + 6].number = grids[i][j].number;
                    ++l;
                }
                else if (i == 7)
                {
                    grids[i + 1][l].number = grids[i][j].number;
                    grids[i - 1][l + 6].number = grids[i][j].number;
                    ++l;
                }
                else if (i == 8)
                {
                    grids[i - 2][l].number = grids[i][j].number;
                    grids[i - 1][l + 6].number = grids[i][j].number;
                    ++l;
                }
            }
        }
    }

}
